package com.niji.littly_donkey_android.http;

import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class AsynHttpClient {

  //volley的请求队列
  private RequestQueue requestQueue;
  //保证全局变量是单一的
  private static AsynHttpClient instance;

  private Context mContext;

  public AsynHttpClient(Context context) {
    requestQueue = Volley.newRequestQueue(context);
    this.mContext = context;
  }
  /**
   * 单列模式，让AsynHttpClient在整个项目中保持只有一个
   * @param context
   * @return
   */
  public static AsynHttpClient getInstance(Context context) {
    if (instance==null){
      synchronized (AsynHttpClient.class){
        if (instance==null){
          instance=new AsynHttpClient(context);
        }
      }
    }
    return instance;
  }

  /**
   * 添加任务到volley的请求队列中去，并不会立即执行
   * @param task
   */
  public void addTask(Request task){
    requestQueue.add(task);
  }

  public RequestQueue getRequestQueue() {
    return requestQueue;
  }
}

